'''
Created on 04/07/2012

@author: Administrador
'''
import unittest
from main.memory import Frame
from main.process import PCB, Instruction, Process, ProcessStatus, IOInstruction, ProcessStates
from main.os import OS_PaginationFactory
from time import sleep

class TestPlanners(unittest.TestCase):
    def setUp(self):
        self.pcb1 =  PCB(3)
        self.pcb2 =  PCB(6)
        self.pcb3 =  PCB(7)
        self.instruction1 =  Instruction(ProcessStatus.CPU,1)#proceso1 es de Cpu
        self.instruction2 =  Instruction(ProcessStatus.CPU,2)
        self.instruction3 =  Instruction(ProcessStatus.CPU,3)
        self.instruction4 =  IOInstruction(4,'Printer')
        self.instruction5 =  Instruction(ProcessStatus.CPU,5)
        self.instruction6 =  IOInstruction(6,'Printer')
        self.instruction7 =  Instruction(ProcessStatus.CPU,7)
        self.instruction8 =  IOInstruction(8,'Printer')
        self.instruction9 =  IOInstruction(9,'Printer')
        self.instruction10=  Instruction(ProcessStatus.CPU,10)
        #------------------------------------------#
        self.process1 = Process(self.pcb1)
        self.process1.addInstruction(self.instruction1)
        self.process1.addInstruction(self.instruction2)
        self.frame1 = Frame(5)
        self.frame1.addInstruction(0,self.instruction1)
        self.frame1.addInstruction(1,self.instruction2)
        self.process1.pcb.processFrames.append(self.frame1)
#        self.process1.addInstruction(self.instruction3)
#        self.process1.addInstruction(self.instruction4)
        #------------------------------------------#
        self.process2 = Process(self.pcb2)
        self.process2.addInstruction(self.instruction5)
        self.process2.addInstruction(self.instruction6)
        self.frame2 = Frame(5)
        self.frame2.addInstruction(0,self.instruction5)
        self.frame2.addInstruction(1,self.instruction6)
        self.process2.pcb.processFrames.append(self.frame2)
#        self.process2.addInstruction(self.instruction7)
        #------------------------------------------#
        self.process3 = Process(self.pcb3)
        self.process3.addInstruction(self.instruction8)
        self.process3.addInstruction(self.instruction9)
        self.frame3 = Frame(5)
        self.frame3.addInstruction(0,self.instruction8)
        self.frame3.addInstruction(1,self.instruction9)
        self.process3.pcb.processFrames.append(self.frame3)
#        self.process3.addInstruction(self.instruction10)
        #------------------------------------------#
        self.os = OS_PaginationFactory.newSJF_E_OS()
        
    def test_GetProcessWithBiggestPriority(self):
        self.os.addNewProcess(self.process1)
        self.os.addNewProcess(self.process2)
        self.os.addNewProcess(self.process3)
        self.os.start()
        sleep(500)
        #los procesos una vez terminados de ejecutar todas sus instrucciones su estado debe ser finnished
        self.assertEquals(self.process1.pcb.status, ProcessStates().FINISHED)
        self.assertEquals(self.process2.pcb.status, ProcessStates().FINISHED)
        self.assertEquals(self.process3.pcb.status, ProcessStates().FINISHED)
        #los procesos una vez terminados de ejecutar todas sus instrucciones
        self.assertEquals(self.process1.pcb.currentInstruction, None)
        self.assertEquals(self.process2.pcb.currentInstruction, None)
        self.assertEquals(self.process3.pcb.currentInstruction, None)
        #si tenemos una cola de procesos terminados podriamos ver si su tamanio es 3
        self.assertEquals(self.os.finishedProcessesQueue.length(),3)
            
            
suite = unittest.TestLoader().loadTestsFromTestCase(TestPlanners)
unittest.TextTestRunner(verbosity=2).run(suite)  